$(function () {
    obj.init();
});

var obj = new function() {
    return {
        applyForm: null,
        form: null,
        type: null,
        init: function () {
            this.type  = xhTool.getQuery('type');
            this.applyForm = $('#apply_plan_form');
            this.form = new xhForm(this.applyForm);
            this.funObj().addData();//获取select数据
            this.listener();
        },
        listener: function () {
            var $this = this;
            $('#apply_form',window.top.document).find('[id="apply_plan_confirm"]').click(function () {
                $this.funObj().saveData();
            });
            //保存
            $('#savePlanApply').click(function () {
                $this.funObj().saveData();
            });
            $('#addCateLogs').click(function () {
                $this.funObj().addCateLogs();
            });
            //检测开始时间
            $('#beginDate').on('change keyup', function () {
                $this.funObj().checkStartNotice($('#beginDate').val());
            });
            //检测结束时间
            $('#endDate').on('change keyup', function () {
                $this.funObj().checkEndNotice($('#endDate').val());
            });
        },
        funObj: function () {
            var form = this.applyForm;
            var $this = this;
            return {
                addData: function () {
                    var _dom = $('#apply_form',window.top.document).find('[id="apply_plan_confirm"]');
                    _dom.text(_dom.text()).attr('disabled', true);
                    xhExtra.ajaxRequest('res/getCatalogsByBookId', {bookId: bookId}, {check:true}, function (data) {
                        $('#demand').select2({placeholder: '选择章节，必选', data: data, allowClear: false, multiple : true});
                        // if (JSON.stringify(data) === '[]'){
                        //     $('#addCateLogs').text('没有章节，不可选').attr('disabled', true);
                        // }
                    });
                    $(".form-date").datetimepicker({
                        format: 'yyyy-mm-dd',
                        language: 'zh-CN',
                        minView: "month",
                        autoclose: true
                    });
                    $('#beginDate').val(maxDate);
                    _dom.text(_dom.text()).attr('disabled', false);
                },
                saveData: function () {
                    var progress = $('#progress').val();
                    var demand = $('#demand').val();
                    var beginDate = $('#beginDate').val();
                    var endDate = $('#endDate').val();

                    xhExtra.ajaxRequest('res/addPlanApply', {
                        id: id,
                        progress: progress,
                        demand: demand,
                        beginDate: beginDate,
                        endDate: endDate,
                        bookId: bookId,
                        type: $this.type
                    }, {success: true,check:false},function (res) {
                        xhExtra.setBtnNormal($('#apply_form',window.top.document).find('[id="apply_plan_confirm"]'));
                        if(res.code === 200){
                            window.top.xhTop.showSuccess(res.msg);
                            // window.top.xhTop.closeActiveTab();
                            $('#apply_form', window.top.document).find('[id="bt_modal_close"]').click();
                            // window.top.xhTop.closeModal();
                        } else {
                            window.top.xhTop.showError(res.error);
                            return false;
                        }
                    });

                    // var check = $this.funObj().checkData(beginDate, endDate);
                    // if (check !== true) {
                    //     var showMsg = '';
                    //     if (check === 1) {
                    //         showMsg = '开始时间不能小于结束时间';
                    //     }
                    //     if (check === 2) {
                    //         showMsg = '时间间隔不能低于10天';
                    //     }
                    // }
                },
                checkData: function (startTime, endTime) {
                    if (startTime.length > 0 && endTime.length > 0) {
                        var startTmp = startTime.split("-");
                        var endTmp = endTime.split("-");
                        var sd = new Date(startTmp[0], startTmp[1], startTmp[2]);
                        var ed = new Date(endTmp[0], endTmp[1], endTmp[2]);
                        if (sd.getTime() > ed.getTime()) {
                            return 1;
                        } else if ((ed.getTime() - sd.getTime()) < 3*24*60*60*1000){//间隔不能小于5天
                            return 2;
                        }
                    }
                    return true;
                },
                addCateLogs: function () {
                    window.top.xhTop.showModal({
                        title: '章节信息',
                        id: 'cate_log_form',
                        footerHtml:
                        '<button type="button" class="btn btn-primary xh-btn-load" id="cate_log_confirm">确认</button>' +
                        '<button type="button" class="btn btn-default" data-dismiss="modal" id="bt_modal_close">关闭</button>',
                        content: xhTool.getFrameString('res/showPlanCateLogTree', {type:$this.type, bookId: bookId}, 500),
                        size: 'lg',
                        width:'950'
                    });
                },
                addCateLog: function (cateLogData) {
                    $('#demand').val(cateLogData).trigger('change');
                },
                checkStartNotice: function (_date) {
                    if (_date) {
                        var check = $this.funObj().checkData(maxDate, _date);
                        var showMsg = '';
                        if (maxDateType === 1) {
                            if (check !== true) {
                                if (check === 1) {
                                    showMsg = '开始时间不能小于最新排期时间'+maxDate;
                                }
                            }
                        } else if(maxDateType === 2) {
                            if (check !== true) {
                                if (check === 1) {
                                    showMsg = '开始时间不能小于当前时间';
                                }
                            }
                        }
                        $('#startNotice').html(showMsg);
                    } else {
                        $('#startNotice').html();
                    }
                },
                checkEndNotice: function (_date) {
                    if (_date) {
                        var check = $this.funObj().checkData($('#beginDate').val(), _date);
                        var showMsg = '';
                        if (check !== true) {
                            if (check === 1) {
                                showMsg = '结束时间不能小于开始时间';
                            } else {
                                showMsg = '间隔不能小于5个工作日';
                            }
                        }
                        $('#endNotice').html(showMsg);
                    } else {
                        $('#endNotice').html();
                    }
                }
            }
        }
    }
};
